package com.heima.nio.bytebuffer;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import static com.heima.nio.bytebuffer.ByteBufferUtil.*;

/**
 * @author ZhengDp
 * @Date 2023/6/22 17:24
 */
public class TestByteBufferString {
    public static void main(String[] args) {
        // 1. 字符串转换为 ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put("hello".getBytes());
        debugAll(buffer);

        // 2. Charset
        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");
        debugAll(buffer2);

        // 3. 利用wrap
        ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
        debugAll(buffer3);

        // ByteBuffer 转换为String
        String s = StandardCharsets.UTF_8.decode(buffer2).toString();
        System.out.println(s);
    }
}
